package com.app.scale.bean;


import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.annotation.JSONField;
import com.app.scale.scale.ScaleActivity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import my.DialogUtils;
import my.MathUtils;
import my.SystemParamsUtils;
import my.TimeUtils;

public class GoodsListBean extends BaseListBean {

    public final   static int  ISGUADAN = 2;   //挂单
    public final   static int  NOTGUADAN = 1;   //正常

    private ArrayList<GoodsListData> list = new ArrayList<GoodsListData>();

    public ArrayList<GoodsListData> getList() {
        return list;
    }

    public void setList(ArrayList<GoodsListData> list) {
        this.list = list;
    }

    @JSONField(serialize = false)
    public void addListBean(GoodsListBean listbean) {
        if (listbean == null)
            return;
        if (this.getCurrentPage() == INIT_PAGE) {
            setList(listbean.getList());
        } else {
            if (list == null)
                list = new ArrayList<GoodsListData>();
            list.addAll(listbean.getList());
        }
        setListBeanData(listbean);
    }


    @JSONField(serialize = false)
    public void addListData(GoodsListData goodsListData) {
        if (list == null) return;
        list.add(goodsListData);
    }


    public List<String> getAllNameList(){
        List<String> nameList = new ArrayList<>();
        if (list.size()>0){
            for (GoodsListData data : list){
                nameList.add(data.getGna());
            }
        }
        return nameList;
    }
//
//    public GoodsListData getCompanyByChoose(int position){
//        return list.get(position);
//    }

    public void deleteCheckList(){
        Iterator<GoodsListData> it = list.iterator();
        while(it.hasNext()){
            GoodsListData bean = it.next();
            if(bean.isCheck()){
                it.remove();
            }
        }
    }

    public int haveGuadan(){
        int sty =GoodsListBean.NOTGUADAN;
        for (GoodsListData goodsListData : list){
            if(goodsListData.isGuadan()){
                sty = GoodsListBean.ISGUADAN;
            }
        }
        return sty;
    }

    public void guaDanCheckList(){
        for (GoodsListData goodsListData : list){
//            if(goodsListData.isCheck()){
                goodsListData.setGpr("0");
                goodsListData.setCheck(false);
                goodsListData.setSty(GoodsListBean.ISGUADAN);
//            }
        }
    }

    public void changeBeanByPosition(int position,GoodsListData otherBean){
        GoodsListData goodsListData = list.get(position);
        goodsListData.setGid(otherBean.getGid());
        goodsListData.setGno(otherBean.getGno());
        goodsListData.setGna(otherBean.getGna());
        goodsListData.setFiles(otherBean.getFiles());
        goodsListData.setFsg(otherBean.getFsg());
        goodsListData.setLev(otherBean.getLev());
    }


    public void changPrice(Context context,String price){
        if(list== null || list.size()==0) return;
        GoodsListData goodsData = null;
        switch (haveMoreSelect()){
            case 0:
                goodsData = list.get(list.size()-1);
                break;
            case 1:
                goodsData =  getSelectBean();
                break;
            default:
                DialogUtils.showErrorDialog(context,"输入单价时候只能选择一个！");
                return;
        }


        if(goodsData.isGuadan()){
            DialogUtils.showErrorDialog(context,"挂单无法输入单价！");
        }else {
            goodsData.setGpr(price);
        }
    }

    public int haveMoreSelect(){
        int value = 0;
        for (GoodsListData data : list){
            if(data.isCheck()){
                value ++;
            }
        }
        return value;
    }


    public GoodsListData getSelectBean(){
        GoodsListData result = null;
        for (GoodsListData data : list){
            if(data.isCheck()){
                result = data;
                break;
            }
        }
        return result;
    }


    public String getAllTotal(){
        String total = "0";
        for (GoodsListData goodsData : list){
            String mult = MathUtils.multiply(goodsData.getGpr(),goodsData.getGwt());
            total =  MathUtils.add(total,mult);
        }
        return total;
    }

    @JSONField(serialize = false)
    public String getAllSje() {
        if (list == null)
            return "0";
        String a = "0";
        for (GoodsListData data : list){
            a = MathUtils.add(a,data.getSje());
        }
        return a;
    }


    @JSONField(serialize = false)
    public String getAllWeight(){
        String weight = "0";
        for (GoodsListData goodsData : list){
            weight =  MathUtils.add(weight,goodsData.getGwt());
        }
        return weight;
    }

    public String getAllTra1Weight(){
        String weight = "0";
        for (GoodsListData goodsData : list){
            if(goodsData.getFsg().equals("银针")){
                weight =  MathUtils.add(weight,goodsData.getGwt());
            }
        }
        return weight;
    }

    public String getAllTra2Weight(){
        String weight = "0";
        for (GoodsListData goodsData : list){
            if(goodsData.getFsg().equals("牡丹")){
                weight =  MathUtils.add(weight,goodsData.getGwt());
            }
        }
        return weight;
    }

    public String getAllTra3Weight(){
        String weight = "0";
        for (GoodsListData goodsData : list){
            if(goodsData.getFsg().equals("寿眉")){
                weight =  MathUtils.add(weight,goodsData.getGwt());
            }
        }
        return weight;
    }


    public GoodsSubmitBean getSubmitBean(ScaleActivity activity,SupplierListData supplierData, String uno){
        GoodsSubmitBean submitBean = new GoodsSubmitBean();
        submitBean.setLeafm(getleafm(activity,supplierData,uno));
        submitBean.setLeafs(getLeafsList(supplierData,uno));
        return submitBean;
    }

    public Leafm getleafm(ScaleActivity activity,SupplierListData supplierData,String uno){
        Leafm leafm = new Leafm();
        leafm.setAid(LoginBean.getInstance().getAcode());
        leafm.setCna(supplierData.getCna());
        leafm.setCno(supplierData.getCno());
        leafm.setCdno(supplierData.getCdno());
        leafm.setCid(supplierData.getCid());
        leafm.setCht(supplierData.getCht());
        leafm.setJno(uno);
        leafm.setTdt(TimeUtils.getCurrentTimeByFormat(TimeUtils.TIME_FOMART_DAY));
        leafm.setTje(getAllTotal());
        leafm.setUno(LoginBean.getInstance().getUno());
        leafm.setWno(SystemParamsUtils.getAndroidID(activity));
        leafm.setSty(haveGuadan());



        switch (supplierData.getCfs()){

            case "代理商":
                String tra1Value = MathUtils.subtract(supplierData.editTra1AddTra1(),getAllTra1Weight());
                if(MathUtils.compareTo(tra1Value,"0") == -1){
                    leafm.setTra1(tra1Value);   //溯源重量
                }

                String tra2Value = MathUtils.subtract(supplierData.editTra2AddTra2(),getAllTra2Weight());
                if(MathUtils.compareTo(tra2Value,"0") == -1){
                    leafm.setTra2(tra2Value);   //溯源重量
                }

                String tra3Value = MathUtils.subtract(supplierData.editTra3AddTra3(),getAllTra3Weight());
                if(MathUtils.compareTo(tra3Value,"0") == -1){
                    leafm.setTra3(tra3Value);   //溯源重量
                }
                break;
            case "茶农":
                leafm.setTra1(MathUtils.subtract(supplierData.getTra1(),getAllTra1Weight()));   //银针溯源重量
                leafm.setTra2(MathUtils.subtract(supplierData.getTra2(),getAllTra2Weight()));   //牡丹溯源重量
                leafm.setTra3(MathUtils.subtract(supplierData.getTra3(),getAllTra3Weight()));   //寿眉溯源重量
                break;
        }

        return leafm;
    }

    public List<Leafs> getLeafsList(SupplierListData supplierData,String jNo){
        List<Leafs>leafsList = new ArrayList<>();
        for(GoodsListData goodsListData:list){
            Leafs leaf = new Leafs();
            leaf.setTdt(TimeUtils.getCurrentTimeByFormat(TimeUtils.TIME_FOMART_DAY));
            leaf.setAid(LoginBean.getInstance().getAcode());
            leaf.setGid(goodsListData.getGid());
            leaf.setGun("斤");
            leaf.setGwt(""+goodsListData.getGwt());
            leaf.setGpr(""+goodsListData.getGpr());
            leaf.setSje(MathUtils.multiply(leaf.getGwt(),leaf.getGpr()));
            leaf.setUno(LoginBean.getInstance().getUno());
            leaf.setJno(jNo);
            leaf.setCno(supplierData.getCno());
            leaf.setGna(goodsListData.getGna());
            leaf.setGno(goodsListData.getGno());
            leaf.setPath(goodsListData.getPath());
            leaf.setSty(goodsListData.getSty());
            leaf.setAcc(goodsListData.getAcc());
            leaf.setPwt(goodsListData.getPwt());
            leaf.setCno(supplierData.getCno());
            leaf.setCdno(supplierData.getCdno());
            leafsList.add(leaf);
        }
        return leafsList;
    }

    public class GoodsSubmitBean extends BaseListBean{



        GoodsListBean.Leafm leafm;
        List<GoodsListBean.Leafs> leafs;

        public GoodsListBean.Leafm getLeafm() {
            return leafm;
        }

        public void setLeafm(GoodsListBean.Leafm leafm) {
            this.leafm = leafm;
        }

        public List<GoodsListBean.Leafs> getLeafs() {
            return leafs;
        }

        public void setLeafs(List<GoodsListBean.Leafs> leafs) {
            this.leafs = leafs;
        }

        public String getTid(){
            if(leafm == null) return "";
            if(TextUtils.isEmpty(leafm.getTid())) return "";
            return leafm.getTid();
        }

        public String getTra1(){
            if(leafm == null) return "";
            if(TextUtils.isEmpty(leafm.getTra1())) return "";
            return leafm.getTra1();
        }
        public String getTra2(){
            if(leafm == null) return "";
            if(TextUtils.isEmpty(leafm.getTra2())) return "";
            return leafm.getTra2();
        }
        public String getTra3(){
            if(leafm == null) return "";
            if(TextUtils.isEmpty(leafm.getTra3())) return "";
            return leafm.getTra3();
        }

        public String getTra1Tra2Tra3(){
            String str ="";
            str+=getTra1();
            str+=getTra2();
            str+=getTra3();
            return str;
        }

    }

    public class  Leafm {

        @JSONField(name = "aid")
        private String aid;
        @JSONField(name = "cid")
        private String cid;
        @JSONField(name = "cdno")
        private String cdno;
        @JSONField(name = "cna")
        private String cna;         //代理商名称
        @JSONField(name = "cno")
        private String cno;
        @JSONField(name = "jna")
        private String jna;
        @JSONField(name = "jno")
        private String jno;         //计价员
        @JSONField(name = "lat")
        private String lat;
        @JSONField(name = "lgt")
        private String lgt;
        @JSONField(name = "tdt")
        private String tdt;     //收茶时间
        @JSONField(name = "tid")
        private String tid;
        @JSONField(name = "tje")
        private String tje;
        @JSONField(name = "tra1")
        private String tra1;
        @JSONField(name = "tra2")
        private String tra2;
        @JSONField(name = "tra3")
        private String tra3;
        @JSONField(name = "una")
        private String una;
        @JSONField(name = "uno")
        private String uno;
        @JSONField(name = "cht")
        private String cht;
        @JSONField(name = "wno")
        private String wno;             //秤台编号
        @JSONField(name = "sty")
        private int sty = 1;             //1:正常  2 挂单

        public String getCid() {
            return cid;
        }

        public void setCid(String cid) {
            this.cid = cid;
        }

        public String getCdno() {
            return cdno;
        }

        public void setCdno(String cdno) {
            this.cdno = cdno;
        }



        public String getCht() {
            return cht;
        }

        public void setCht(String cht) {
            this.cht = cht;
        }

        public int getSty() {
            return sty;
        }

        public void setSty(int sty) {
            this.sty = sty;
        }
        public String getAid() {
            return aid;
        }

        public void setAid(String aid) {
            this.aid = aid;
        }

        public String getCna() {
            return cna;
        }

        public void setCna(String cna) {
            this.cna = cna;
        }

        public String getCno() {
            return cno;
        }

        public void setCno(String cno) {
            this.cno = cno;
        }

        public String getJna() {
            return jna;
        }

        public void setJna(String jna) {
            this.jna = jna;
        }

        public String getJno() {
            return jno;
        }

        public void setJno(String jno) {
            this.jno = jno;
        }

        public String getLat() {
            return lat;
        }

        public void setLat(String lat) {
            this.lat = lat;
        }

        public String getLgt() {
            return lgt;
        }

        public void setLgt(String lgt) {
            this.lgt = lgt;
        }

        public String getTdt() {
            return tdt;
        }

        public void setTdt(String tdt) {
            this.tdt = tdt;
        }

        public String getTid() {
            return tid;
        }

        public void setTid(String tid) {
            this.tid = tid;
        }

        public String getTje() {
            return tje;
        }

        public void setTje(String tje) {
            this.tje = tje;
        }


        public String getUna() {
            return una;
        }

        public void setUna(String una) {
            this.una = una;
        }

        public String getUno() {
            return uno;
        }

        public void setUno(String uno) {
            this.uno = uno;
        }

        public String getWno() {
            return wno;
        }

        public void setWno(String wno) {
            this.wno = wno;
        }

        public String getTra1() {
            return tra1;
        }

        public void setTra1(String tra1) {
            this.tra1 = tra1;
        }

        public String getTra2() {
            return tra2;
        }

        public void setTra2(String tra2) {
            this.tra2 = tra2;
        }

        public String getTra3() {
            return tra3;
        }

        public void setTra3(String tra3) {
            this.tra3 = tra3;
        }
    }

    public class  Leafs {
        @JSONField(name = "cid")
        private String cid;
        @JSONField(name = "cdno")
        private String cdno;
        @JSONField(name = "aid")
        private String aid;
        @JSONField(name = "cno")
        private String cno;
        @JSONField(name = "gid")
        private String gid;
        @JSONField(name = "gun")
        private String gun;
        @JSONField(name = "gwt")
        private String gwt;
        @JSONField(name = "gpr")
        private String gpr;
        @JSONField(name = "jno")
        private String jno;
        @JSONField(name = "sje")
        private String sje;
        @JSONField(name = "swt")
        private String swt;
        @JSONField(name = "tdt")
        private String tdt;
        @JSONField(name = "gna")
        private String gna;
        @JSONField(name = "gno")
        private String gno;
        @JSONField(name = "uno")
        private String uno;
        @JSONField(name = "path")
        private String path;
        @JSONField(name = "sty")
        private int sty = 1;             //1:正常  2 挂单
        @JSONField(name = "acc")
        private int acc = 1;   //累加
        @JSONField(name = "pwt")
        private String pwt;   //去皮

        public String getCid() {
            return cid;
        }

        public void setCid(String cid) {
            this.cid = cid;
        }

        public String getCdno() {
            return cdno;
        }

        public void setCdno(String cdno) {
            this.cdno = cdno;
        }

        public String getPwt() {
            return pwt;
        }

        public void setPwt(String pwt) {
            this.pwt = pwt;
        }

        public int getSty() {
            return sty;
        }

        public void setSty(int sty) {
            this.sty = sty;
        }

        public int getAcc() {
            return acc;
        }

        public void setAcc(int acc) {
            this.acc = acc;
        }

        public String getAid() {
            return aid;
        }

        public void setAid(String aid) {
            this.aid = aid;
        }

        public String getCno() {
            return cno;
        }

        public void setCno(String cno) {
            this.cno = cno;
        }

        public String getGid() {
            return gid;
        }

        public void setGid(String gid) {
            this.gid = gid;
        }


        public String getGpr() {
            return gpr;
        }

        public void setGpr(String gpr) {
            this.gpr = gpr;
        }


        public String getGun() {
            return gun;
        }

        public void setGun(String gun) {
            this.gun = gun;
        }

        public String getGwt() {
            return gwt;
        }

        public void setGwt(String gwt) {
            this.gwt = gwt;
        }

        public String getJno() {
            return jno;
        }

        public void setJno(String jno) {
            this.jno = jno;
        }

        public String getSje() {
            return sje;
        }

        public void setSje(String sje) {
            this.sje = sje;
        }

        public String getSwt() {
            return swt;
        }

        public void setSwt(String swt) {
            this.swt = swt;
        }

        public String getTdt() {
            return tdt;
        }

        public void setTdt(String tdt) {
            this.tdt = tdt;
        }

        public String getUno() {
            return uno;
        }

        public void setUno(String uno) {
            this.uno = uno;
        }

        public String getGna() {
            return gna;
        }

        public void setGna(String gna) {
            this.gna = gna;
        }

        public String getGno() {
            return gno;
        }

        public void setGno(String gno) {
            this.gno = gno;
        }
        public String getPath() {
            return path;
        }

        public void setPath(String path) {
            this.path = path;
        }

    }


}
